﻿using MonitorPlatform.Common;
using MonitorPlatform.DataAccess;
using MonitorPlatform.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MonitorPlatform.ViewModel
{
    public class DevicePageViewModel
    {
        public ObservableCollection<CategoryItemModel> CategoryDevice {  get; set; }
        public ObservableCollection<CategoryItemModel> CategoryDeviceState { get; set; }
        public ObservableCollection<DeviceModel> DeviceList { get; set; }
        private List<DeviceModel> DeviceAll = new List<DeviceModel>();
        public CommandBase StatusFilterCommand { get; set; }

        public DevicePageViewModel() 
        {
            this.StatusFilterCommand = new CommandBase();
            this.StatusFilterCommand.DoCanExecute = new Func<object, bool>((o)=>true);
            this.StatusFilterCommand.DoExecute= new Action<object>(DoFilter);
            this.InitCategory();
            this.InitDeviceList();
        }

        private void DoFilter(object o)
        {
            string status = o.ToString();
            List<DeviceModel> temp = DeviceAll;
            if (status != "全部")
            {
                temp = DeviceAll.Where(c => c.DeviceStatus.Equals(status)).ToList();
            }

            DeviceList.Clear();
            foreach (var item in temp)
            {
                DeviceList.Add(item);
            }

        }

        private void InitCategory()
        {
            this.CategoryDevice = new ObservableCollection<CategoryItemModel>();
            this.CategoryDevice.Add(new CategoryItemModel("全部", true));
            this.CategoryDevice.Add(new CategoryItemModel("液位计"));
            this.CategoryDevice.Add(new CategoryItemModel("流量计"));

            this.CategoryDeviceState = new ObservableCollection<CategoryItemModel>();
            this.CategoryDeviceState.Add(new CategoryItemModel("全部", true));
            this.CategoryDeviceState.Add(new CategoryItemModel("正常"));
            this.CategoryDeviceState.Add(new CategoryItemModel("报警"));
        }

        private void InitDeviceList()
        {
            DeviceList = new ObservableCollection<DeviceModel>();
            for (int i = 0; i < 10; i++)
            {
                DeviceList.Add(new DeviceModel { IsShowSkeleton = true });
            }
            Task.Run(new Action(async () =>
            {
                this.DeviceAll.Add(new DeviceModel("液位计一", "液位计", "正常", "https://img2.baidu.com/it/u=2690410767,3186017725&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800", "描述信息"));
                this.DeviceAll.Add(new DeviceModel("液位计二", "液位计", "正常", "https://img2.baidu.com/it/u=2690410767,3186017725&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800", "描述信息"));

                this.DeviceAll.Add(new DeviceModel("液位计三", "液位计", "正常", "https://img2.baidu.com/it/u=2690410767,3186017725&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800", "描述信息"));

                this.DeviceAll.Add(new DeviceModel("液位计四", "液位计", "报警", "https://img2.baidu.com/it/u=2690410767,3186017725&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800", "描述信息"));
                this.DeviceAll.Add(new DeviceModel("流量计", "流量计", "正常", "https://img2.baidu.com/it/u=2690410767,3186017725&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800", "描述信息"));
                
                await Task.Delay(300);

                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    DeviceList.Clear();
                    foreach (var item in DeviceAll)
                    {
                        DeviceList.Add(item);
                    }
                }));
            }));

            // this.DeviceList = new ObservableCollection<DeviceModel>();
            

        }
    }
}
